package com.swjd.service;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author liutao
 */
@Service
public class ScheduledTaskService {
    private static final SimpleDateFormat DATE_FORMAT=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private Integer count1=1;
    private Integer count2=1;
    private Integer count3=1;
    @Scheduled(fixedRate = 60000) //每分钟执行一次
    public void scheduledTaskImmediately(){
        System.out.println(String.format("fixeRate第%s次执行，当前时间为：%s",count1++,DATE_FORMAT.format(new Date())));
    }
    @Scheduled(fixedDelay = 60000) //从上个定时任务间隔一分钟执行
    public void scheduledTaskAfterSleep() throws InterruptedException {
        System.out.println(String.format("fixedDelay第%s次执行，当前时间为：%s",count2++,DATE_FORMAT.format(new Date())));
    Thread.sleep(10000);
    }
    @Scheduled(cron = "0 * * * * *")
    public void scheduledTaskCron(){
        System.out.println(String.format("cron第%s次执行，当前时间为：%s",count3++,DATE_FORMAT.format(new Date())));
    }

}
